<?php


namespace app\common\model;

use app\common\model\Banner;
use app\common\model\CateGoods;
use app\common\model\Goods;
use EasyWeChat\OfficialAccount\ShakeAround\PageClient;
use think\Db;
use think\Model;
class Index extends Model
{

    // 首页信息
    public function indexInfo()
    {
        $time       = time();
        $start_time = date('Y-m-d 00:00:00',time());
        $end_time   = date('Y-m-d 23:59:59',time());
        // 获取 url
        $url    = config('fastadmin.api_url');
        // 轮播图
        $banner = banner::where('status',1)->field('id,image')->order('create_time desc')->select();
        foreach($banner as $key => $val)
        {
            $banner[$key]['image'] = $url.$val['image'];
        }
        // 商品分类
        $cate_goods = CateGoods::where(['status'=>1,'pid'=>0])->field('id,title,image')->select();
        foreach($cate_goods as $key => $val)
        {
            $cate_goods[$key]['image'] = $url.$val['image'];
        }
        // 限时秒杀
        $seckill_goods  = Db::name('goods')
            ->alias('g')
            ->join('goods_seckill gs','g.id = gs.good_id')
            ->field('g.id,g.name,g.image,g.item,g.integer,g.create_time,end_time')
            ->where(['g.activity'=>1])
            ->where('g.start_time','<=',$time)
            ->where('g.end_time','>=',$time)
            ->order('g.create_time desc')
            ->select();

        if($seckill_goods)
        {
            // 获取倒计时时间
            ksort($seckill_goods);
            $countdown = current($seckill_goods);
            $countdowns['start_time'] = time();
            $countdowns['end_time']   = $countdown['end_time'];
        }else{
            $countdowns = [];
        }
        
        if($seckill_goods)
        {
            foreach($seckill_goods as $key => $val)
            {
                $item = json_decode($val['item'],true);
                $item = current($item);
                $seckill_goods[$key]['money'] = $item['intro'];
                $seckill_goods[$key]['integer'] = $item['integral'];
                $seckill_goods[$key]['image'] = $url.$val['image'];
                if(!isset($val1['vip_money']))$seckill_goods[$key]['vip_money'] = 0;
            }
        }
        // 是否推荐
        $recom = [['id'=>0,'title'=>'推荐']];
        $goods = Db::name('cate_goods')->where('pid','>',0)->where('status',1)->field('id,title')->select();
        // 合并数组
        $merge = array_merge($recom,$goods);
        foreach($merge as $key => &$val)
        {
            if(empty($val['id']))
            {
                $val['goods'] = Db::name('goods')->where(['state'=>2,'type'=>1,'status'=>1])->field('id,image,name,integer,money,item,integer')->select();
                foreach($val['goods'] as &$val1)
                {
                    $item = json_decode($val1['item'],true);
                    $item = current($item);
                    $val1['money'] = $item['intro'];
                    $val1['integer'] = $item['integral'];
                    $val1['image'] = $url.$val1['image'];
                    !isset($item['vip_money']) ? $val1['vip_money']= 0 : $val1['vip_money']= $item['vip_money'];
                }
            }else{
                $val['goods'] = Db::name('goods')->where(['cate_id'=>$val['id'],'type'=>1,'status'=>1])->field('id,image,name,integer,money,item,integer')->select();
                foreach($val['goods'] as &$val1)
                {
                    $item = json_decode($val1['item'],true);
                    $item = current($item);
                    $val1['money'] = $item['intro'];
                    $val1['integer'] = $item['integral'];
                    $val1['image'] = $url.$val1['image'];
                    !isset($item['vip_money']) ? $val1['vip_money']= 0 : $val1['vip_money']= $item['vip_money'];
                }
            }
        }

        $result = ['banner'=>$banner,'cate_goods'=>$cate_goods,'seckill_goods'=>$seckill_goods,'goods'=>$merge,'countdowns'=>$countdowns];
        return $result;
    }

    /*
     * 协议
     * */
    public function agreements()
    {
        $privacy  = Db::name('config')->where('id',25)->value('value');
        $business = Db::name('config')->where('id',26)->value('value');
        $res = ['privacy'=>$privacy,'business'=>$business];
        return $res;
    }

    /*
     * 地区
     * */
    public function areas()
    {
        $field    = ['id','name','pid'];
        $province = Db::name('area')->where('level',1)->field($field)->select();
        $city     = Db::name('area')->where('level',2)->field($field)->select();
        $area     = Db::name('area')->where('level',3)->field($field)->select();
        $res      = ['province'=>$province,'city'=>$city,'area'=>$area];
        return $res;
    }

    /*
     * 秒杀商品
     * */
    public function seckills($data)
    {
        $url  = config('fastadmin.api_url');
        $time = time();
        $start_time = date('Y-m-d 00:00:00',time());
        $end_time   = date('Y-m-d 23:59:59',time());
        $page = $data['page'] ? $data['page'] : 1;
        $data = Db::name('goods_seckill')
            ->alias('gs')
            ->join('goods g','g.id = gs.good_id')
            ->where(['g.activity'=>1])
            ->where('g.start_time','<=',$time)
            ->where('g.end_time','>=',$time)
            ->field('g.id,g.name,g.image,g.item,g.integer,g.create_time,g.start_time,g.end_time')
            ->order('g.create_time desc')
            ->page($page,10)
            ->select();
        if($data)
        {
            foreach($data as &$val)
            {
                $image = explode(',',$val['image']);
                $val['image'] = $url.$image[0];
                $val['item']  = json_decode($val['item'],true);
            }
            // 获取倒计时时间
            ksort($data);
            $countdown = current($data);
            $countdowns['start_time'] = time();
            $countdowns['end_time']   = $countdown['end_time'];
        }
        return ['data'=>$data,'countdown'=>$countdowns];
    }



    /*
     * 商品搜索
     */
    public function indexSearch($params)
    {
        $url  = config('fastadmin.api_url');
        $page = isset($params['page']) && $params['page'] ? $params['page'] : 1;
        $row_list = isset($params['row_list']) && $params['row_list'] ? $params['row_list'] : 10;
        $where = [];
        if(isset($params['name']) && $params['name'])
        {
            $where['name'] = ['like','%'.$params['name'].'%'];
        }else{
            $where['id']   = ['=',0];
        }
        $data = Db::name('goods')->where($where)->where(['is_del'=>1,'type'=>1,'status'=>1])->field('id,name,image,item,activity,start_time,end_time')->page($page,$row_list)->select();
        $ms   = Db::name('goods_seckill')->column('good_id');
        
        $res  = [];
        // if($data)
        // {
        //     foreach($data as $key => $val)
        //     {
        //         $item = json_decode($val['item'],true)[0];
        //         $data[$key]['money'] = $item['intro'];
        //         $data[$key]['integral'] = $item['integral'];
        //         $data[$key]['image'] = $val['image'] ? $url.$val['image'] : "";
        //         isset($val['money']) ? $data[$key]['vip_money'] = $val['money'] : $data[$key]['vip_money'] = '';
        //         unset($data[$key]['item']);
                
        //         if(in_array($val['id'],$ms))
        //         {
        //             if($val['end_time'] <= time())
        //             {
        //                 unset($data[$key]);
        //             }
        //         }
            
        //     }
        // }
        
        if($data)
        {
            foreach($data as $key => $val)
            {
                $item = json_decode($val['item'],true)[0];
                $val['money'] = $item['intro'];
                $val['integral'] = $item['integral'];
                $val['image'] = $val['image'] ? $url.$val['image'] : "";
                isset($val['money']) ? $val['vip_money'] = $val['money'] : $val['vip_money'] = '';
                unset($val['item']);
                
                if(in_array($val['id'],$ms))
                {
                    if($val['end_time'] <= time())
                    {
                        unset($val);
                    }
                }else{
                    $res[] = $val;
                }
            }
        }
        
        return $res;
    }
    
    
    
    
    
    
}